<?php


namespace app\index\validate;


use think\Validate;

class Customer extends Validate
{
    protected $rule = [
        ['id','require|number','客户id不能为空|客户id必须为数字'],
        ['pro_id','require','商品不能为空'],
        ['group','require','所属组不能为空'],
        ['cn_name','require','商品中文名不能为空'],
        ['en_name','require','商品英文名不能为空'],
        ['type','require|number','计量单位不能为空'],
        ['amount','require|number','数量不能为空|数量必须为数字'],
        ['weight','requireIf:type,2|number','重量不能为空|重量必须为数字'],
        ['selling_price','require|number','售价不能为空|售价必须为数字'],
        ['customer_name','require','客户名称不能为空'],
        ['nick_name','require','账户名称不能为空'],
        ['email','require|email','邮箱不能为空|邮箱格式不正确'],
        ['country','require','国家不能为空'],
        ['city','require','城市不能为空'],
        ['address','require','地址不能为空'],
        ['home_page','require','主页地址不能为空'],
        ['zip_code','require','邮编不能为空'],
        ['phone','require','电话不能为空'],
        ['product','require|array|requireProductArray:(input:\'product\')','商品信息必填|商品信息必须为数组|商品信息不能为空'],
        ['detail_name','requireIf:pro_id,0|checkDetailArray:(input:\'product\')','请填写详细信息|详细信息不能为空'],
    ];

    protected $scene = [
        'customer_order'  =>  ['pro_id','group','type','en_name','amount','selling_price','customer_name'],
        'customer_order_product'  =>  ['pro_id','cn_name','type','en_name','amount'],
        'customer_order_batch_product'=>['pro_id','type','en_name','amount','selling_price',],
        'customer_order_batch'  =>  ['group','customer_name','product'],
        'customer_order_update'  =>  ['pro_id','group','type','en_name','amount','selling_price','customer_name'],
        'create'  =>  ['customer_name','group','nick_name','email'],
        'update'  =>  ['id','customer_name','group','nick_name','email'],
    ];

    protected function requireProductArray($value){
        $rules = [
            ['pro_id', 'require', '商品不能为空'],
            ['en_name', 'require', '商品英文名不能为空'],
            ['type', 'require', '计量单位不能为空'],
            ['selling_price','require|number','售价不能为空|售价必须为数字'],
            ['amount','require|number','数量不能为空|数量必须为数字'],
//            ['children','requireIf:pro_id,0|array|requireDetailArray:(input:\'children\')','请填写详细信息|详细信息必须为数组|详细信息不能为空'],
        ];

        foreach ($value as $key=>$item) {
            if(!is_array($item)){
                return false;
            }
            $data=[];
            foreach ($item as $i=>$val) {
                $data[$i] = $val;
            }
            if (false === $this->check($data, $rules)) {
                return $this->getError();
            }
        }
        return true;
    }

    protected function checkDetailArray($value){
        $rules = [
            ['pro_id', 'require|number|gt:0', '详细信息商品不能为空|详细信息商品不能为数字|详细信息商品必须为已存在商品'],
            ['en_name', 'require', '详细信息商品英文名不能为空'],
            ['cn_name', 'require', '详细信息商品中文名不能为空'],
            ['type', 'require', '详细信息计量单位不能为空'],
            ['amount','require|number','详细信息数量不能为空|详细信息数量必须为数字'],
        ];

        foreach ($value as $key=>$item) {
            if(!is_array($item)){
                return false;
            }
            if($item['delete']===true) {
                continue;
            }
            $data=[];
            foreach ($item as $i=>$val) {
                $data[$i] = $val;
            }
            if (false === $this->check($data, $rules)) {
                return $this->getError();
            }
        }
        return true;
    }
}